<?php

namespace App\Http\Controllers;

use App\Traits\ResponseTrait;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\ValidationException;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests, ResponseTrait;

    //请求参数验证
    public function validateRequest(
        Request $request,
        array $rules,
        array $messages = [],
        array $customAttributes = []
    ) {
        try{
            $messages = array_merge($messages,[
                'required' => '参数 :attribute 不能为空',
                'required_if' => '参数 :attribute 不能为空',
                'json' => '参数 :attribute 不是合法的json字符串',
                'array' => '参数 :attribute 不是数组',
            ]);
            $this->validate($request,$rules,$messages,$customAttributes);
        }catch (ValidationException $e){
            Log::error('请求参数验证错误：'.$e->validator->errors()->first());
            throw new \Exception($e->validator->errors()->first());
        }
    }
}
